﻿using Basic.OAModel.DB;
using Basic.OARemoteModel;
using Basic.OARemoteModel.DataFillIn.Model;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.Model;
using WeDonekRpc.SqlSugar;

namespace Basic.OADAL.Repository
{
    internal class EmpDataFillInDAL : BasicDAL<DBEmpDataFillIn, long>, IEmpDataFillInDAL
    {
        public EmpDataFillInDAL ( IRepository<DBEmpDataFillIn> basicDAL ) : base(basicDAL)
        {
        }
        public long Add ( FillInAdd data )
        {
            DBEmpDataFillIn add = data.ConvertMap<FillInAdd, DBEmpDataFillIn>();
            add.Id = IdentityHelper.CreateId();
            add.CreateTime = DateTime.Now;
            add.Status = FillInStatus.起草;
            this._BasicDAL.Insert(add);
            return add.Id;
        }

        public void SubmitAudit ( long id, int fillTotal )
        {
            if ( !this._BasicDAL.Update(new DBEmpDataFillIn
            {
                Id = id,
                Status = FillInStatus.审核中,
                FillTotal = fillTotal
            }, new string[] { "Status", "FillTotal" }) )
            {
                throw new ErrorException("oa.fill.audit.submit.fail");
            }
        }

        public Result[] Query<Result> ( FillInQuery query, IBasicPage paging, out int count ) where Result : class
        {
            paging.InitOrderBy("Id", true);
            return this._BasicDAL.Query<Result>(query.ToWhere(), paging, out count);
        }

        public void Stop ( long id )
        {
            if ( !this._BasicDAL.Update(a => a.Status == FillInStatus.停用, a => a.Id == id) )
            {
                throw new ErrorException("oa.fill.stop.fail");
            }
        }

        public void SetAuditResult ( long id, FillInStatus status )
        {
            if ( !this._BasicDAL.Update(new DBEmpDataFillIn
            {
                Id = id,
                Status = status,
                AuditEndTime = DateTime.Now,
            }, new string[] { "Status", "AuditEndTime" }) )
            {
                throw new ErrorException("oa.fill.status.set.fail");
            }
        }
    }
}
